<?php

declare(strict_types=1);

namespace DreamCat\Array2Class\TestCases;

use DreamCat\Array2Class\Array2ClassConverter;
use DreamCat\Array2Class\Demo\HasEnumPopo;
use DreamCat\Array2Class\Demo\Inner\CommonEnum;
use DreamCat\Array2Class\Demo\Inner\IntEnum;
use DreamCat\Array2Class\Demo\Inner\StringEnum;
use DreamCat\Array2Class\Exception\ConvertFailed;
use DreamCat\Array2Class\Utils\FeatureJudge;
use PHPUnit\Framework\TestCase;

/**
 * 测试内置枚举
 * @author vijay
 */
class EnumTest extends TestCase
{
    /**
     * 测试枚举
     * @return void
     */
    public function testEnum()
    {
        if (!FeatureJudge::hasEnum()) {
            self::assertEquals("", "");
            return;
        }
        $data = ["e1" => "V1", "e2" => 3, "e3" => "v2"];
        $convert = new Array2ClassConverter();
        $actual = $convert->convert($data, HasEnumPopo::class);
        $expected = new HasEnumPopo();
        $expected->e1 = CommonEnum::V1;
        $expected->e2 = IntEnum::Int3;
        $expected->e3 = StringEnum::Str2;
        self::assertEquals($expected, $actual);
    }

    /**
     * 测试枚举无有效值的情况
     * @return void
     */
    public function testInvalidEnum()
    {
        if (!FeatureJudge::hasEnum()) {
            self::assertEquals("", "");
            return;
        }
        $data = ["e1" => "V11", "e2" => 3, "e3" => "v2"];
        try {
            $convert = new Array2ClassConverter();
            $convert->convert($data, HasEnumPopo::class);
            $noError = true;
        } catch (ConvertFailed $convertFailed) {
            $noError = false;
            self::assertEquals(CommonEnum::class . " 不支持输入的值 V11", $convertFailed->getMessage());
        }
        self::assertFalse($noError);
    }
}
